package com.vipheyue.livegamecontrol;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.vipheyue.livegamecontrol.bean.MyUser;
import com.vipheyue.livegamecontrol.bean.OperationBean;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class RechargeActivity extends AppCompatActivity {

    @Bind(R.id.et_username)
    EditText et_username;
    @Bind(R.id.et_userpassword)
    EditText et_userpassword;
    @Bind(R.id.ben_login)
    Button ben_login;
    @Bind(R.id.et_rechageMoney)
    EditText et_rechageMoney;
    @Bind(R.id.btn_recharge)
    Button btn_recharge;
    @Bind(R.id.tv_userMoney)
    TextView tv_userMoney;
    private MyUser localUser;
    private int oldMoney;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge);
        ButterKnife.bind(this);


    }

    @OnClick({R.id.ben_login, R.id.btn_recharge})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ben_login:
                login();
                break;
            case R.id.btn_recharge:
                final int rechargeMoney=Integer.parseInt(et_rechageMoney.getText().toString().trim());
                localUser.setMoney(localUser.getMoney() +rechargeMoney );
                localUser.update(this, new UpdateListener() {
                    @Override
                    public void onSuccess() {
                        Toast.makeText(RechargeActivity.this, "充值成功", Toast.LENGTH_SHORT).show();
                        tv_userMoney.setText("充值成功!新增积分:"+et_rechageMoney.getText().toString().trim());
                        btn_recharge.setEnabled(false);
                        OperationBean bean=new OperationBean();
                        bean.setIncreaseMoneey(rechargeMoney);
                        bean.setOldMoney(oldMoney);
                        bean.setNewMoney(localUser.getMoney());
                        bean.save(RechargeActivity.this, new SaveListener() {
                            @Override
                            public void onSuccess() {
                                localUser.logOut(RechargeActivity.this);

                            }

                            @Override
                            public void onFailure(int i, String s) {

                            }
                        });

                    }

                    @Override
                    public void onFailure(int i, String s) {
                        Toast.makeText(RechargeActivity.this, "充值失败" + i + s, Toast.LENGTH_SHORT).show();
                    }
                });
                break;
        }
    }

    private void login() {
        BmobUser.loginByAccount(this, et_username.getText().toString().trim(), et_userpassword.getText().toString().trim(), new LogInListener<MyUser>() {

            @Override
            public void done(MyUser user, BmobException e) {
                if (user != null) {
                    Log.i("smile", "用户登陆成功");
                    btn_recharge.setEnabled(true);
                    localUser = user;
                    tv_userMoney.setText("用户积分: "+localUser.getMoney());
                    oldMoney = localUser.getMoney();
                } else {
                    Toast.makeText(RechargeActivity.this, "账户用户名或密码错误:" + e.getMessage() + e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        localUser.logOut(RechargeActivity.this);
        super.onDestroy();
    }
}
